<?php

	require_once "trackingModify.php";
	
	$blacklist = array('.php', '.phtml');

	//*********************************************
	// get Info from controll
	//*********************************************
	$ID = $_POST['showItems'];
	$id_type = $_POST['lTypes'];
	$name = $cFilter ->filter($_POST['tf_name']);
	$price = $cFilter->filter ($_POST['tf_price']);
	$status_price = $_POST['lPrice'];
	$status_sell = $cFilter -> filter($_POST['tf_status_sell']);
	
	$script_performance ="";
	
	if(!is_int($status_sell))
	{
		$status_sell = -1;
	}
	
	
	$description = $_POST['tf_description'];
	$manufacture = $cFilter ->filter($_POST['tf_manufacture']);
	$id_item = $cFilter -> filter($_POST['tf_idItems']);
	
	$status = $_POST['rdbtn_status'];
	
		

	$picture = $_FILES['tf_filePicture'];
	/*
	$thumbnail = $_FILES['tf_fileThumbnail'];
	*/
	
	//*********************************************
	// verify conditions of file uploaded
	//*********************************************
	$pictureFilePath ="";
	
	$pictureInfo = NULL;
	if(basename($picture['name']) !="" )
	{
		$pictureFilePath = "../resources/".time()."_".basename($picture['name']);		
		$pictureInfo = getimagesize($picture['tmp_name']);
	}
	
	
	$thumbnailFilePath = "";
	/*$thumbnailInfo =NULL;
	
	if(basename($thumbnail['name']) !="")
	{
		$thumbnailFilePath  = "../resources/".time()."_".basename($thumbnail['name']);
		$thumbnailInfo = getimagesize($thumbnail['tmp_name']);
	}
	
	*/
	
	
	//*********************************************
	// verify conditions of required field
	//*********************************************
	$errorType = -1;

	if($id_type <= 0 || !is_numeric($id_type))
	{
		// 0 se mac dinh cho loi chua chon type
		$errorType = 0;
		
		
		echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbedi&idbpos='.$ID.'&notification=ITEM_ACTION_EDIT_ERROR_TYPE"</script>'; 				
		return;
	}
	
	if($name === "")
	{
		// 1 se mac dinh cho loi khong co name
		$errorType = 1;
		echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbedi&idbpos='.$ID.'&notification=ITEM_ACTION_EDIT_ERROR_NAME"</script>'; 				
		return;
	}
	
	
	if($price =="" || !is_numeric ($price))
	{
		echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbedi&idbpos='.$ID.'&notification=ITEM_ACTION_EDIT_ERROR_PRICE"</script>'; 
		
		return;			
	}
	
	if($id_item =="")
	{
		echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbedi&idbpos='.$ID.'&notification=ITEM_ACTION_EDIT_ERROR_ITEMCODE"</script>';   
		
		return ;				
	}
	//*********************************************
	// execute update if all conditions are ok
	//*********************************************
	if($errorType == -1)
	{	
		$item = new CItems ;
		
		$thumbnail_type='vertical_type';
		
		if($picture['name'] !="")
		{
			list($width, $height) = getimagesize($picture['tmp_name']);
			if($width>$height)
				$thumbnail_type='horizontal_type';
		}	
		$isSuccess  = $item ->CItemsUpdate($ID,$id_type,$name,$price,$status_price,$status_sell,$status,$pictureFilePath,$thumbnail_type,$description,$manufacture,$script_performance,$id_item);
		
		/*-------------------------------------------------------
		//
		// Xu ly upload File 
		//
		--------------------------------------------------------*/
		
		if($picture['name'] =="")
		{
			echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbview&notification=ITEM_ACTION_EDIT_ERROR_PICTURE2"</script>';  
		}
		else
		{
			if(	$pictureFilePath !="" &&
				( $picture['type'] == 'image/gif' || $picture['type'] == 'image/jpeg' || $picture['type'] == 'image/png' ) 
				&& 
				( $pictureInfo['mime'] =='image/gif' || $pictureInfo['mime'] == 'image/jpeg' ||  $pictureInfo['mime'] == 'image/png' ) 
				&& 
				!preg_match('/$blacklist[0]\$/i', $picture['name']) 
				&& 
				!preg_match('/$blacklist[1]\$/i', $picture['name'])				
				) 
				{				
					move_uploaded_file($picture['tmp_name'], $pictureFilePath);
						
				}
				else
				{
					echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbview&notification=ITEM_ACTION_EDIT_ERROR_PICTURE"</script>';  
					//echo "file picture upload khong thoa dieu kien";
					
					
					 return ;
				}
		
		}
		
		
		

		if($isSuccess ==-1)
		{
			
			
			echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbview&notification=XXXX_ACTION_TO_SERVER"</script>';  
			
		}
		else
		{
			if($isSuccess !=0)
			{
				echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbview&notification=ITEM_ACTION_EDIT_SUCCESS"</script>';
				
			}
		}	
			
		
	}
	
			
	





?>